// https://leetcode.cn/problems/missing-number/
// Created by ade on 2022/7/25.
//
#include <iostream>
#include <vector>
#include <algorithm>
#include <unordered_map>

using namespace std;

class Solution {
public:
    // 异或
    int missingNumber1(vector<int> &nums) {
        int ans = 0;
        for (int i = 0; i <= nums.size(); i++) ans ^= i;
        for (auto i : nums) ans ^= i;
        return ans;
    }

    // 差值
    int missingNumber2(vector<int> &nums) {
        int n = nums.size();
        int sum = n * (n + 1) / 2;
        for (auto i : nums) sum -= i;
        return sum;
    }

    // 原地hash
    int missingNumber(vector<int> &nums) {
        unordered_map<int, int> myMap = {};
        for (auto i : nums) {
            myMap[i] = 1;
        }
        for (int i = 0; i < nums.size(); i++) {
            if (myMap.find(i) == myMap.end()) {
                return i;
            }
        }
        return nums.size();

    }
};

int main() {
    vector<int> nums = {9, 6, 4, 2, 3, 5, 8, 0, 1};
    Solution so;
    int a = so.missingNumber(nums);
    cout << a << endl;
    return 0;
}
